<?php
/**
 * Library Of Shared Code (LOSC)
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   LOSC Framework
 * @package    Validate
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */

/**
 * Validator for checking that a string is a valid locale
 *
 * @category   LOSC Framework
 * @package    Validate
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */
class Losc_Validate_Locale extends Zend_Validate_Abstract
{
    const NOT_LOCALE = 'notLocale';

    /**
     * Error messages
     * 
     * @var array
     */
    protected $_messageTemplates = array(
        self::NOT_LOCALE => '"%value%" does not appear to be a locale'
    );
    
    /**
     * Whether strict should be used in {@link Zend_Locale::isLocale()}
     *
     * @var bool
     */
    protected $_strict;
    
    /**
     * Creates validator, optionally setting strict mode
     *
     * @param bool $strict  [optional] whether strict mode should be used,
     *                      defaults to true
     */
    public function __construct($strict = true)
    {
        $this->_strict = (bool) $strict;
    }
    
    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value is a valid locale
     * 
     * @param  string $value
     * @return boolean
     */
    public function isValid($value)
    {
        $this->_setValue($value);

        if (!Zend_Locale::isLocale($value, $this->_strict)) {
            $this->_error();
            return false;
        }

        return true;
    }
}
